//
//  SignInViewController.m
//  chongwu
//
//  Created by 隼目 on 2021/9/15.
//

#import "SignInViewController.h"
@interface SignInViewController ()<UITextFieldDelegate>

@property(nonatomic,strong)UITextField *phoneText;
@property(nonatomic,strong)UITextField *codeText;
@property(nonatomic,strong)UIButton *codeBtn;

@property(nonatomic,assign)int timeTick;
@property(nonatomic,strong)NSTimer *timer;

@end

@implementation SignInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden=YES;

    [self  creatView];
}

-(void)creatView{
    
    UIImageView *logoImage=[UIImageView new];
    [self.view addSubview:logoImage];
    [logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_mainHeight*179);
        make.centerX.mas_equalTo(0);
        make.width.height.mas_equalTo(141.9*_mainWidth);
    }];
    logoImage.layer.masksToBounds=YES;
    logoImage.layer.cornerRadius=40*_mainWidth;
    logoImage.image=[UIImage imageNamed:@"logo"];
    
    UILabel *nameLabel=[UILabel new];
    [self.view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(logoImage.mas_bottom).mas_equalTo(43*_mainHeight);
    }];
    nameLabel.text=@"欢迎使用爱宠!";
    nameLabel.font = [UIFont fontWithName:@"PingFang SC" size:autoScaleW(41)];
    nameLabel.textColor=rgba(44,46,52,1);
    nameLabel.textAlignment=NSTextAlignmentCenter;
    
    self.phoneText=[UITextField new];
    [self.view addSubview:self.phoneText];
    [self.phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30*_mainWidth);
        make.right.mas_equalTo(-30*_mainWidth);
        make.top.mas_equalTo(nameLabel.mas_bottom).mas_equalTo(100*_mainHeight);
        make.height.mas_equalTo(94*_mainHeight);
    }];
    self.phoneText.backgroundColor=rgba(245,247,249,1);
    self.phoneText.layer.masksToBounds=YES;
    self.phoneText.layer.cornerRadius=8*_mainWidth;
    //CGRectMake(0, 0,15, 0)离上下左右的距离
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,_mainWidth*30, 0)];
    self.phoneText.leftView = paddingView;
    self.phoneText.leftViewMode = UITextFieldViewModeAlways;
    self.phoneText.placeholder=@"请输入手机号";
    self.phoneText.delegate=self;
    self.phoneText.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(34)];
    
    
    self.codeText=[UITextField new];
    [self.view addSubview:self.codeText];
    [self.codeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30*_mainWidth);
        make.right.mas_equalTo(-304*_mainWidth);
        make.top.mas_equalTo(self.phoneText.mas_bottom).mas_equalTo(40*_mainHeight);
        make.height.mas_equalTo(94*_mainHeight);
    }];
    self.codeText.backgroundColor=rgba(245,247,249,1);
    self.codeText.layer.masksToBounds=YES;
    self.codeText.layer.cornerRadius=8*_mainWidth;
    //CGRectMake(0, 0,15, 0)离上下左右的距离
    UIView *paddingView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0,_mainWidth*30, 0)];
    self.codeText.leftView = paddingView2;
    self.codeText.leftViewMode = UITextFieldViewModeAlways;
    self.codeText.placeholder=@"请输入验证码";
    self.codeText.delegate=self;
    self.codeText.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(34)];

    self.codeBtn=[UIButton new];
    [self.view addSubview:self.codeBtn];
    [self.codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-30*_mainWidth);
        make.centerY.mas_equalTo(self.codeText);
        make.height.mas_equalTo(87*_mainHeight);
        make.width.mas_equalTo(243*_mainWidth);
    }];
    [self.codeBtn setTitle:@"获取验证码" forState:0];
    [self.codeBtn  setTitleColor:rgba(255,102,0,1) forState:0];
    self.codeBtn.titleLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(32)];
    self.codeBtn.layer.masksToBounds=YES;
    self.codeBtn.layer.borderColor=rgba(255,102,0,1).CGColor;
    self.codeBtn.layer.borderWidth=1;
    self.codeBtn.layer.cornerRadius=87*_mainHeight/2;
    [self.codeBtn addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIImageView *bottomImage=[UIImageView new];
    [self.view addSubview:bottomImage];
    [bottomImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(_mainW/1662*1062);
    }];
    bottomImage.image=[UIImage imageNamed:@"dengluye"];
    
    
    UIButton *signInBtn=[UIButton new];
    [self.view addSubview:signInBtn];
    [signInBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40*_mainWidth);
        make.right.mas_equalTo(-40*_mainWidth);
        make.top.mas_equalTo(self.codeText.mas_bottom).mas_equalTo(_mainHeight*80);
        make.height.mas_equalTo(_mainHeight*90);
    }];
    [signInBtn setTitle:@"登录" forState:0];
    [signInBtn setTitleColor:[UIColor whiteColor] forState:0];
    signInBtn.titleLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(32)];
    
    [signInBtn layoutIfNeeded];
    signInBtn.layer.masksToBounds=YES;
    signInBtn.layer.cornerRadius=90*_mainHeight/2;
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = signInBtn.bounds;
    //将CAGradientlayer对象添加在我们要设置背景色的视图的layer层
    [signInBtn.layer insertSublayer:gradientLayer atIndex:0];
    //设置渐变区域的起始和终止位置（范围为0-1）
    gradientLayer.startPoint = CGPointMake(0, 1);
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.colors = @[(__bridge id)rgba(255,159,0,1).CGColor,
                             (__bridge id)rgba(255,116,0,1).CGColor ,
                             (__bridge id)rgba(255,102,0,1).CGColor];
    //设置颜色分割点（范围：0-1）
    gradientLayer.locations = @[@(0),@(0.5), @(1.0f)];
    signInBtn.layer.shadowColor = rgba(196,50,0,0.36).CGColor;
    signInBtn.layer.shadowOffset = CGSizeMake(0,0);
    signInBtn.layer.shadowOpacity = 1;
    signInBtn.layer.shadowRadius = 4;
    [signInBtn addTarget:self action:@selector(selectSignInBtn) forControlEvents:UIControlEventTouchUpInside];
}
-(void)selectSignInBtn{
    [self.view endEditing:YES];
    
    NSString *phoneStr=self.phoneText.text;
    
    if (![ZQTools isMobileNumber:phoneStr]) {
        [ZQTools svpInfo:@"请输入正确的手机号"];
        return;
    }
    NSString *codeStr=self.codeText.text;
    if ([ZQTools charIsNil:codeStr]) {
        [ZQTools svpInfo:@"请输入验证码"];
        return;
    }
    NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithCapacity:0];
    [dic setValue:phoneStr forKey:@"phone"];
    [dic setValue:codeStr forKey:@"code"];
    [self PostWithUrl:@"/member/login" param:dic tableView:nil success:^(id  _Nullable dict) {
        
        
        if ([NSKeyedArchiver archiveRootObject:dict[@"content"] toFile:_userModelFile]) {
            [self pushToMian];
            [ZQTools svpInfo:@"登录成功"];
        }
        
        
    
        NSLog(@"%@",dict);
    } failure:^(NSError * _Nullable error) {
        
    }];
}

-(void)getCode{
    [self.view endEditing:YES];
    
    NSString *phoneStr=self.phoneText.text;
    
    if (![ZQTools isMobileNumber:phoneStr]) {
        [ZQTools svpInfo:@"请输入正确的手机号"];
        return;
    }
    NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithCapacity:0];
    [dic setValue:phoneStr forKey:@"phone"];
   
    [self getWithUrl:@"/member/verify/code" param:dic tableView:nil success:^(id  _Nullable dict) {
        
//        NSLog(@"%@",dict);
        self.timeTick=60;
        self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
        [ZQTools svpInfo:@"发送短信成功"];
//        
    } failure:^(NSError * _Nullable error) {
        
    }];
    
}
-(void)timeFireMethod
{
    if (_timeTick==0) {
        [self.codeBtn setTitle:@"获取验证码" forState:0];
        [self.codeBtn  setTitleColor:rgba(255,102,0,1) forState:0];
        self.codeBtn.layer.borderColor=rgba(255,102,0,1).CGColor;
        self.codeBtn.enabled=YES;
        [_timer invalidate];
        return;
    }
    _timeTick--;
    
    [self.codeBtn setTitle:[NSString stringWithFormat:@"%ds后重新发送",_timeTick] forState:0];
    self.codeBtn.enabled=NO;
    [self.codeBtn setTitleColor:rgba(170, 170, 170, 1) forState:0];
    self.codeBtn.layer.borderColor=rgba(170,170,170,1).CGColor;
    
}

-(void)pushToMian{
    MailViewController *mail=[[MailViewController alloc] init];
    mail.isHidenBackgroundImage=YES;
    mail.navigationItem.title=@"寄件";
    OrderViewController *order=[[OrderViewController alloc] init];
    order.isHidenBackgroundImage=YES;
    MessageViewController *message=[[MessageViewController alloc] init];
    message.isHidenDog=YES;
    MyViewController *my=[[MyViewController alloc] init];
    my.isHidenBackgroundImage=YES;
    my.navigationItem.title=@"我的";

    UINavigationController *nav1= [[UINavigationController alloc] initWithRootViewController:mail];
    UINavigationController *nav2= [[UINavigationController alloc] initWithRootViewController:order];
    UINavigationController *nav3= [[UINavigationController alloc] initWithRootViewController:message];
    UINavigationController *nav4= [[UINavigationController alloc] initWithRootViewController:my];

    nav1.title=@"寄件";
    nav2.title=@"订单";
    nav3.title=@"消息";
    nav4.title=@"我的";
    nav3.navigationItem.title=@"消息";
    

    [nav1.tabBarItem setSelectedImage:[UIImage imageNamed:@"jijian"]];
    [nav1.tabBarItem setImage:[UIImage imageNamed:@"jijianhui"]];

    [nav2.tabBarItem setSelectedImage:[UIImage imageNamed:@"dingdan"]];
    [nav2.tabBarItem setImage:[UIImage imageNamed:@"dingdanhui"]];

    [nav3.tabBarItem setSelectedImage:[UIImage imageNamed:@"xiaoxi"]];
    [nav3.tabBarItem setImage:[UIImage imageNamed:@"xiaoxihui"]];

    [nav4.tabBarItem setSelectedImage:[UIImage imageNamed:@"wode"]];
    [nav4.tabBarItem setImage:[UIImage imageNamed:@"wodehui"]];

    [[UITabBar appearance] setTintColor:rgba(255,102,0,1)];
    
    UITabBarController *bar=[[UITabBarController alloc] init];
    bar.viewControllers=@[nav1,nav2,nav3,nav4];
    self.view.window.rootViewController=bar;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
